import * as types from "./mutation-types";
import Api from "api/api";

const state = {
  slidesList: {},
  currentSlide: {},
  searchCondition: {
    pageSize: 10,
    type: ""
  }
};

const getters = {
  slidesList: state => state.slidesList,
  currentSlide: state => state.currentSlide,
  searchCondition: state => state.searchCondition
};

const actions = {
  getSlidePageS({ commit }, param) {
    return Api.getSlidePage(param).then(ret => {
      commit(types.UPDATE_SLIDE_LIST, ret.data.data);
      return ret;
    });
  },
  getSlideS({ commit }, id) {
    return Api.getSlide({ id: id }).then(ret => {
      commit(types.UPDATE_CURRENT_SLIDE, ret.data.data);
      return ret;
    });
  },
  addOrUpdateSlideS({ commit }, payload) {
    return Api.addOrUpdateSlide({ body: payload }).then(ret => {
      return ret;
    });
  },

  deleteSlideS({ commit }, param) {
    return Api.deleteSlide(param).then(ret => {
      return ret;
    });
  }
};

const mutations = {
  [types.INIT_CURRENT_SLIDE](state, payload) {
    state.currentSlide = {
      content: "",
      image: "",
      orderIndex: 0,
      title: "",
      type: "homeScroll"
    };
  },
  [types.UPDATE_CURRENT_SLIDE](state, payload) {
    state.currentSlide = payload;
  },
  [types.UPDATE_SLIDE_LIST](state, payload) {
    state.slidesList = payload;
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
